using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Stock.Dto;
using JTZY.Model.Stock;
using JTZY.Repository;
using JTZY.Service.Stock.IStockService;

namespace JTZY.Service.Stock
{
    /// <summary>
    /// 库存明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IProdStockService), ServiceLifetime = LifeTime.Transient)]
    public class ProdStockService : BaseService<ProdStock>, IProdStockService
    {
        /// <summary>
        /// 查询库存明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ProdStockDto> GetList(ProdStockQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<ProdStock, ProdStockDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ProdStock GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加库存明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ProdStock AddProdStock(ProdStock model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 导出库存明细
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ProdStockDto> ExportList(ProdStockQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .Select((it) => new ProdStockDto()
                {
                }, true)
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<ProdStock> QueryExp(ProdStockQueryDto parm)
        {
            var predicate = Expressionable.Create<ProdStock>();
            predicate = predicate.And(it => it.UserId == parm.UserId);
            return predicate;
        }
    }
}